package com.android.BluethootChess.ui;

import android.graphics.Point;

import com.android.BluethootChess.model.Square;
import com.android.BluethootChess.model.SquarePosition;
import com.android.BluethootChess.util.Utils;

public class UISquares {
	public final Square[][] squares;
	private int sizeX; // Latimea unui patrat de pe tabla.
	private int sizeY; // Inaltimea unui patrat de pe tabla.

	public UISquares() {
		squares = new Square[Utils.BOARD_SIZE][Utils.BOARD_SIZE];
		sizeX = Utils.BOARD_WIDTH / Utils.BOARD_SIZE;
		sizeY = Utils.BOARD_HEIGHT / Utils.BOARD_SIZE;
		initSquares();
	}

	/**
	 * Creaza o lista cu patrate.
	 * */
	private void initSquares() {
		for (int i = 0; i < squares.length; i++) {
			for (int j = 0; j < squares[i].length; j++) {
				squares[i][j] = new Square(new Point(j * sizeX, i * sizeY),
						new Point((j + 1) * sizeX, (i + 1) * sizeY));
			}
		}
	}

	/**
	 * Gaseste coordonatele matriceale ale punctului point
	 * 
	 * @param point
	 *            - punctul pentru care face cautarea
	 * @return - pozitia patratului pe care s-a facut click, sau null daca nu
	 *         exista patrat
	 * */
	public SquarePosition findPointSquare(Point point) {
		for (int i = 0; i < Utils.BOARD_SIZE; i++) {
			for (int j = 0; j < Utils.BOARD_SIZE; j++) {
				if (squares[i][j].belongToSquare(point)) {
					return new SquarePosition(i, j);
				}
			}
		}
		return null;
	}

	public Square[][] getSquares() {
		return squares;
	}

}
